/****************************************************************
 ** Product  :   HP Subscriber Network Application Policy
 ** Module   :   gr-manager
 ** Date: Apr 10, 2015               
 ** Author: Chen zhenhua
 ** (C) Copyright 2015, Hewlett-Packard Company, All Rights Reserved.
 ** This software is the proprietary information of HP, Inc.  
 ** Use is subject to license terms.
 *****************************************************************
 ** Description:   
 **   Sync the role swap info in the remote deployment.
 **    
 ****************************************************************/
package com.hp.snap.gr.action.impl;

import com.hp.snap.gr.exception.ActionException;
import com.hp.snap.gr.model.jaxb.ActionType;
import com.hp.snap.gr.utils.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.Map;

/**
 * 
 * Sync the role swap info in the remote deployment.
 * 
 * 
 * <P>
 * Version: 3.3 <br>
 * Author: Chen Zhenhua
 * Date: Apr 10, 2015
 * 
 * </P>
 * 
 **/
public class SyncGlobalConfigToRemoteActionHandler extends
        AbstractActionHandler {

    private static String tempGloabl = Constants.USER_GR_TEMP + File.separator + "GR-Global.config.temp";


    private static Logger logger = LoggerFactory.getLogger(SyncGlobalConfigToRemoteActionHandler.class);

    @Override
    public void execute(ActionType action,
                        Map<String, Object> actionPlanInputs,
                        Map<String, Object> actionPlanOutputs) throws ActionException {

        String currentSite = Utils.getConfigurationValue(Constants.CFG_GRL_DEPLOY_NAME);
        String remoteSite = Utils.getCorrespondSiteName(currentSite);
        String remoteGRIP = Utils.getGRHostIP(remoteSite);
        String remoteFile = GRMConfigManager.getGRConfigPath();
        String eIUM_home = Utils.getConfigurationValue(Constants.CFG_GRL_eIUM_HOME);
        String eIUM_user = Utils.getConfigurationValue(Constants.CFG_GRL_eIUM_USER);
        String eIUM_pwd = Utils.getConfigurationValue(Constants.CFG_GRL_eIUM_PWD);
        String sgr_data_root =  Utils.getConfigurationValue(Constants.CFG_GRG_ROOT, Constants.CFG_GRG_SGR_DATA_ROOT);

        try {

            String ssConfigFile = GRMConfigManager.getGRConfigPath();
            if (!new File(ssConfigFile).isFile()) {
                SSConfigUtils.exportGRConfig();
            }

            SSConfigUtils.readSSConfig(ssConfigFile);

            boolean result = false;
            result = Utils.swapMasterSlaveRole(Constants.CFG_GRG_MASTER_SLAVE_RELATION, Constants.CFG_GRG_ROOT);
            if (result) {
                SSConfigUtils.saveSSConfig(ssConfigFile);
            }

            SSHTools ssh = new SSHTools(null, null);
            ssh.copyFromLocal(ssConfigFile, remoteGRIP, remoteFile);

            String command = Constants.USER_GR_ARTIFACT + File.separator + "ImportGRGlobalConfig.sh " + eIUM_home + " " + eIUM_user + " " + eIUM_pwd + " " + remoteFile + " " + sgr_data_root;

            String results = ssh.cmd(remoteGRIP, command);
            int status = ssh.getExitStatusOfCommandResult(results);
            if (status != 0) {
                logger.error("execute the ImportGRGlobalConfig.sh to import GR configuration in {} failed!", remoteGRIP);
                throw new ActionException("execute the ImportGRGlobalConfig.sh to import GR configuration in " + remoteGRIP + " failed!");
            }
            ssh.cmd(remoteGRIP, "rm -f " + GRMConfigManager.getGRGlobalConfigPath());

        } catch (Exception e) {
            logger.error("Sync the GR config file to swap role operation failed, please swap master-slave role manually!!");
            logger.error(e.toString());
            throw new ActionException(e.getMessage(), e.getCause());
        }

    }

}
